package com.quentinbeauperin.twitbird
{
	import com.greensock.TweenNano;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ScreenEngine extends Sprite
	{
		private var game:Game;
		
		public var screenBg:Bitmap;
		public var screenBg2:Bitmap;
		public var screen1:Screen;
		public var screen2:Screen;
		public var screen3:Screen;
		public var screen4:Screen;
		public var screen5:Screen;
		
		public var boutonMenu11:Bouton;
		public var boutonMenu12:Bouton;
		public var boutonMenu13:Bouton;
		public var boutonMenu14:Bouton;
		
		public var boutonMenu1:Bouton;
		public var boutonMenu2:Bouton;
		public var boutonMenu3:Bouton;
		public var boutonMenu4:Bouton;
		
		public var buttonControl1:Bouton;
		public var buttonControl2:Bouton;
		public var buttonStory1:Bouton;
		public var buttonStory2:Bouton;
		
		public var id:int = new int();
		
		public function ScreenEngine()
		{
			super();
			
			screenBg = new Assets.ScreenBg();
			addChild(screenBg);
			screen1 = new Screen(1);
			boutonsMenuListener();
			addChild(screen1);
		}
		
		// Listen to the menu's boutons 
		private function boutonsMenuListener():void
		{
			screen1.boutonMenu1.addEventListener(MouseEvent.MOUSE_UP,LaunchGame)
			screen1.boutonMenu2.addEventListener(MouseEvent.MOUSE_UP,Instructions)
			screen1.boutonMenu3.addEventListener(MouseEvent.MOUSE_UP,Options)
			screen1.boutonMenu4.addEventListener(MouseEvent.MOUSE_UP,Credits)
		}
		
		// Listen to the game screen's boutons
		private function boutonsGameListener():void
		{
			screen2.buttonReturn.addEventListener(MouseEvent.MOUSE_UP,LaunchMenuRight);
			screen2.boutonGame1.addEventListener(MouseEvent.MOUSE_UP,Game1);
			screen2.boutonGame2.addEventListener(MouseEvent.MOUSE_UP,Game2);
			screen2.boutonGame3.addEventListener(MouseEvent.MOUSE_UP,Game3);
			screen2.boutonGame4.addEventListener(MouseEvent.MOUSE_UP,Game4);
			screen2.boutonGame5.addEventListener(MouseEvent.MOUSE_UP,Game5)
		}
		
		// Launch the level 1
		private function Game1(event:MouseEvent):void
		{
			game = new Game(1);
			addChild(game);
		}
		// Launch the level 2
		private function Game2(event:MouseEvent):void
		{
			game = new Game(2);
			addChild(game);
		}
		// Launch the level 3
		private function Game3(event:MouseEvent):void
		{
			game = new Game(3);
			addChild(game);
		}
		// Launch the level 4
		private function Game4(event:MouseEvent):void
		{
			game = new Game(4);
			addChild(game);
		}
		// Launch the level 5
		private function Game5(event:MouseEvent):void
		{
			game = new Game(5);
			addChild(game);
		}
		
		// Launch the menu by the right
		private function LaunchMenuRight(event:MouseEvent):void
		{
			screen1 = new Screen(1);
			screen1.x = 800;
			addChild(screen1);
			TweenNano.to(screen1,1,{x:0});
			if (id == 2){
				TweenNano.to(screen2,1,{x:-800});
			}
			else if (id == 3){
				TweenNano.to(screen3,1,{x:-800});
			}
			boutonsMenuListener();
		}
		
		// Bring the menu by the left
		private function LaunchMenuLeft(event:MouseEvent):void
		{
			screen1 = new Screen(1);
			screen1.x = -800;
			addChild(screen1);
			TweenNano.to(screen1,1,{x:0});
			if (id == 4){
				TweenNano.to(screen4,1,{x:800});
			}
			else if (id == 5){
				TweenNano.to(screen5,1,{x:800});
			}
			boutonsMenuListener();
		}
		
		// Bring the level selection's menu
		private function LaunchGame(event:MouseEvent):void
		{
			screen2 = new Screen(2);
			screen2.x = -800;
			addChild(screen2);
			TweenNano.to(screen1,1,{x:800});
			TweenNano.to(screen2,1,{x:0});
			id = 2;
			boutonsGameListener();
		}
		
		// Bring the instructions' screen
		private function Instructions(event:MouseEvent):void
		{
			screen3 = new Screen(3);
			screen3.x = -800;
			addChild(screen3);
			TweenNano.to(screen1,1,{x:800});
			TweenNano.to(screen3,1,{x:0});
			id = 3;
			screen3.buttonStory2.addEventListener(MouseEvent.MOUSE_UP, HandleTabs);
			screen3.buttonReturn.addEventListener(MouseEvent.MOUSE_UP,LaunchMenuRight);
		}
		
		private function HandleTabs(me:MouseEvent):void
		{
			if(me.stageX>200){
				screen3.buttonStory2.removeEventListener(MouseEvent.MOUSE_UP, HandleTabs);
				screen3.removeChild(buttonControl1);
				screen3.removeChild(buttonStory2);
				screen3.addChild(buttonControl2);
				screen3.addChild(buttonStory1);
				screen3.buttonControl2.addEventListener(MouseEvent.MOUSE_UP, HandleTabs);
			}
		}
		
		// Bring the options' screen
		private function Options(event:MouseEvent):void
		{
			screen4 = new Screen(4);
			screen4.x = 800;
			addChild(screen4);
			TweenNano.to(screen1,1,{x:-800});
			TweenNano.to(screen4,1,{x:0});
			id = 4;
			screen4.buttonReturn.addEventListener(MouseEvent.MOUSE_UP,LaunchMenuLeft);
		}
		
		// Bring the credits' screen
		private function Credits(event:MouseEvent):void
		{
			screen5 = new Screen(5);
			screen5.x = 800;
			addChild(screen5);
			TweenNano.to(screen1,1,{x:-800});
			TweenNano.to(screen5,1,{x:0});
			id = 5;
			screen5.buttonReturn.addEventListener(MouseEvent.MOUSE_UP,LaunchMenuLeft);
		}
	}
}